import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup
from xmw.tianya.bot import *
from xmw.dispatcher.event import *
from xmw import tianya
import xmw
from xmw import error
from xmw.tianya import tianya_msg_utils


import os
import urllib2, urllib
import uuid
import logging

import global_setting

from google.appengine.ext import webapp



class MainPage(xmw.RequestHandler):
    def get_thread(self, status):
        q = Bot_Thread.all()
        q.filter('status = ', status)
        thread = q.get()
        return thread
    def select_RUN_thread(self):
        thread = self.get_thread(xmw.Thread_Status.RUN)
        
        if thread is None:
            return None
        else:
            if thread.offset < thread.page_cnt:
                logging.log( logging.INFO, "RUN thread:%s"% thread.thread_id)
                return thread
            else:
                return None
        
    def select_SYN_thread(self):
        thread=self.get_thread(xmw.Thread_Status.SYN)
        if thread is not None:
            logging.log( logging.INFO, "SYN thread:%s"% thread.thread_id)
            
        return thread
    
    def get_RUN_thread_page(self):
        thread=self.select_RUN_thread()
        if thread is None:
            return (None,None)
        
        page = thread.walk_to_page(thread.offset+1)
        return (thread,page)
        
    def get_SYN_thread_page(self):
        thread=self.select_SYN_thread()
        if thread is None:
            return (None,None)
        else:
            page = thread.get_last_page()
            return (thread,page)    
    
    def send_msg(self,post):
        msg_dict={
                  "author":post.author,\
                  "thread_id":post.thread_id,\
                  "post_id" :post.post_id,\
                  "date":post.date,\
                  "url":post.url,\
                  "sender":global_setting.addr,\
                  "name":MsgName.NOTIFY_POST
                  }
        helper=tianya_msg_utils.NOTIFY_POST_Helper()
        (msg,e) =helper.build_msg(msg_dict)
        if msg is None:
            self.render_error(e)
            return
        msg.put()
        pass
    def get(self):         
        """
            how to select the page:
                if RUN thread,should scan the offset page
                if SYN thread,should scan the 
        """                       
        (thread,page)=self.get_RUN_thread_page()
        
        if thread is None and page is None:
            (thread,page)   =   self.get_SYN_thread_page()
        
        if thread is None and page is None:
            e=xmw.error.NO_THREAD_TO_TRACE("scan_post")
            self.render_error(e)
            return
        
        
        latest_post_date    =   thread.latest_post_date
                
        lst_post    =   page.get_lst_post()
        logging.info("read %d posts"%len(lst_post))
        put_cnt=0
        for post in lst_post:
            post_date   =   post.date
            
            if post_date > latest_post_date :
                post.put()
                self.send_msg(post)
                put_cnt=put_cnt+1
                thread.latest_post_date=post_date
                thread.put()
                
            
        
        thread.offset = thread.offset + 1
        thread.put()
        
        e=error.OK("thread id:%s,offset:%d,read post:%d,put post %d,latest_post_date:%s"%(thread.thread_id,thread.offset,len(lst_post),put_cnt,str(thread.latest_post_date)))
        self.render_error(e)
        return
    
def main():
    application = webapp.WSGIApplication(
                                       [('/robot/scan_post', MainPage)],
                                       debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
